package com.wude.guild;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 提取流量
 */
public class ExtractTrafficActivity extends TitleBaseActivity {
    @BindView(R.id.tv_userPhone)
    TextView tv_userPhone;

    @BindView(R.id.ivChoosed)
    ImageView ivChoosed;
    @BindView(R.id.ivChoosedTwo)
    ImageView ivChoosedTwo;
    @BindView(R.id.ivChoosedThree)
    ImageView ivChoosedThree;
    @BindView(R.id.ivChoosedFour)
    ImageView ivChoosedFour;
    @BindView(R.id.tv_allFlow)
    TextView tv_allFlow;//当前用户总流量
    @BindView(R.id.tv_num)
    TextView tv_num;
    @BindView(R.id.tv_numTwo)
    TextView tv_numTwo;
    @BindView(R.id.tv_numThree)
    TextView tv_numThree;
    @BindView(R.id.tv_numFour)
    TextView tv_numFour;
    @BindView(R.id.sv_content)
    ScrollView sv_content;
    private String flow;

    private List<TextView> viewList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_traffic);
        ButterKnife.bind(this);

        setHeaderTitle("提取流量");
        HttpCom.POST(mHandler, HttpCom.getGetUserFlowListw(), null, false);

        Map<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser().token);
        HttpCom.POST(userHandler, HttpCom.getGetUserInfo(), map, false);
        initdata();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    @SuppressLint("HandlerLeak")
    Handler userHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        JSONObject data = jsonObject.getJSONObject("data");
                        String flow = data.getString("flow");
                        tv_allFlow.setText("当前账户有流量：" + flow + "M");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络错误");
                    break;
            }
        }
    };

    private void initdata() {
        UserInfo loginUser = Utils.getLoginUser();
        tv_userPhone.setText(loginUser.phone);

        viewList.add(tv_num);
        viewList.add(tv_numTwo);
        viewList.add(tv_numThree);
        viewList.add(tv_numFour);
    }

    @OnClick({R.id.rl_choosed, R.id.rl_choosedTwo, R.id.rl_choosedThree, R.id.rl_choosedFour, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_choosed:
            case R.id.rl_choosedTwo:
            case R.id.rl_choosedThree:
            case R.id.rl_choosedFour:
                setChoosed(view.getId());
                break;
            case R.id.tv_commit://提取
                Map<String, String> dataMap = new HashMap<>();
                dataMap.put("token", Utils.getLoginUser().token);
                dataMap.put("flow", flow);
                dataMap.put("phone", Utils.getLoginUser().phone);
                HttpCom.POST(handler, HttpCom.getGetUserFlowReceive(), dataMap, false);
                break;
        }
    }

    private void setChoosed(int viewId) {
        ivChoosed.setVisibility(View.GONE);
        ivChoosedTwo.setVisibility(View.GONE);
        ivChoosedThree.setVisibility(View.GONE);
        ivChoosedFour.setVisibility(View.GONE);
        switch (viewId) {
            case R.id.rl_choosed:
                ivChoosed.setVisibility(View.VISIBLE);
                flow = tv_num.getText().toString();
                break;
            case R.id.rl_choosedTwo:
                ivChoosedTwo.setVisibility(View.VISIBLE);
                flow = tv_numTwo.getText().toString();
                break;
            case R.id.rl_choosedThree:
                ivChoosedThree.setVisibility(View.VISIBLE);
                flow = tv_numThree.getText().toString();
                break;
            case R.id.rl_choosedFour:
                ivChoosedFour.setVisibility(View.VISIBLE);
                flow = tv_numFour.getText().toString();
                break;
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONArray data = object.getJSONArray("data");
                        flow = data.get(0).toString();
                        for (int i = 0; i < data.length(); i++) {
                            viewList.get(i).setText(data.get(i).toString() + "M");
                        }
                        sv_content.setVisibility(View.VISIBLE);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    break;
            }
        }
    };

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    //流量提取成功  ---   刷新用户信息
//                    ToastUtils.showLongToast("提取成功！请耐心等待");
                    HttpUtils.DNSGengxin(msg.obj.toString());
                    try {
                        JSONObject object = new JSONObject(msg.obj.toString());
                        JSONObject data = object.getJSONObject("data");
                        tv_allFlow.setText("当前账户有流量：" + data.getString("all_flow") + "M");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    ToastUtils.showLongToast("网络异常！");
                    break;
            }
        }
    };
}
